#ifndef FRONTEND_AUDIO_H
#define FRONTEND_AUDIO_H

//#define AUDIO_WRITE_TO_FILE

#include <stdint.h>

// workaround for msvc which doesn't support c11 atomics
#ifdef _MSC_VER
#define atomic_size_t size_t
#else
#include <stdatomic.h>
#endif

#ifdef AUDIO_WRITE_TO_FILE
#include <stdio.h>
#endif

typedef struct AudioDriver {
    void* handle;

#ifdef AUDIO_WRITE_TO_FILE
    FILE* _test;
    size_t _samples_written;
#endif

    int sample_rate;
    int channel_count;

    unsigned int _buffer_frame_count;
    atomic_size_t queue_write;
    atomic_size_t queue_read;
    float* audio_queue;
} AudioDriver;

/// Initialize the audio driver.
/// Returns a non-zero value if unsuccessful.
extern int audio_init(AudioDriver* audio);

/// Destroy the audio driver.
extern void audio_destroy(AudioDriver* audio);

/// Queue audio into the driver.
/// Returns a non-zero value if unsuccessful.
extern int audio_queue(AudioDriver* audio, float* samples, size_t buffer_size);

/// Return the number of samples available to be queued.
extern size_t audio_available(const AudioDriver* audio);

#endif